package com.fr.report.script.function;

import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;

public class MID extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    if (paramArrayOfObject.length < 3)
      return Primitive.ERROR_NAME;
    Object localObject1 = paramArrayOfObject[0];
    int i = -1;
    Object localObject2 = paramArrayOfObject[1];
    if (localObject2 instanceof Number)
      i = ((Number)localObject2).intValue();
    int j = -1;
    Object localObject3 = paramArrayOfObject[2];
    if (localObject3 instanceof Number)
      j = ((Number)localObject3).intValue();
    if ((i < 1) || (j < 0))
      return Primitive.ERROR_VALUE;
    if (localObject1 != null)
    {
      String str = "" + localObject1;
      if (i > str.length())
        return "";
      if (i + j - 1 > str.length())
        return str.substring(i - 1);
      return str.substring(i - 1, i + j - 1);
    }
    return Primitive.ERROR_VALUE;
  }

  public Function.Type getType()
  {
    return Function.TEXT;
  }

  public String getCN()
  {
    return "MID(text,start_num,num_chars): 返回文本串中从指定位置开始的一定数目的字符，该数目由用户指定。\nText:包含要提取字符的文本串。\nStart_num:文本中需要提取字符的起始位置。文本中第一个字符的start_num为1，依此类推。\nNum_chars:返回字符的长度。\n备注:\n    如果start_num大于文本长度，MID函数返回“”（空文本）。\n    如果start_num小于文本长度，并且start_num加上num_chars大于文本长度，MID函数将从start_num指定的起始字符直至文本末的所有字符。\n    如果start_num小于1，MID函数返回错误信息*VALUE!。\n    如果num_chars是负数，MID函数返回错误信息*VALUE!。\n示例:\nMID(\"Finemore software\",10,8)返回“software”。\nMID(\"Finemore software\",30,5)返回“”（空文本）。\nMID(\"Finemore software\",0,8)返回*VALUE!。\nMID(\"Finemore software\",5,-1)返回*VALUE!。";
  }

  public String getEN()
  {
    return "MID(text,start_num,num_chars): MID returns a specific number of characters from a text string, starting at the position you specify, based on the number of characters you specify.\nText is the text string containing the characters you want to extract.\nStart_num is the position of the first character you want to extract in text. The first character in text has start_num 1, and so on.\nNum_chars specifies the number of characters you want MID to return from text.\n\nRemarks:\n1. If start_num is greater than the length of text, MID returns \"\" (empty text).\n2. If start_num is less than the length of text, but start_num plus num_chars exceeds the length of text, MID returns the characters up to the end of text. \n3. If start_num is less than 1, MID returns the #VALUE! error value. \n4. If num_chars is negative, MID returns the #VALUE! error value.\n\nExample:\n   MID(\"Finemore software\",10,8) = \"software\"\n   MID(\"Finemore software\",30,5) = \"\" (empty text)\n   MID(\"Finemore software\",0,8) = *VALUE!\n   MID(\"Finemore software\",5,-1) = *VALUE!";
  }
}